package com.wcarmon.life.command{

    import com.adobe.cairngorm.commands.ICommand
    import com.adobe.cairngorm.control.CairngormEvent;
	import mx.controls.Alert;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.IResponder;
	import com.wcarmon.life.model.LifeModelLocator;
	import com.wcarmon.life.event.*;
	import com.wcarmon.life.business.*;
	import com.wcarmon.life.vo.*;
	import mx.utils.ObjectUtil;
		
	
	public class ShowReviewCommand implements ICommand, IResponder {
		include "../../includes/loggerSnippet.as";
		
		public function execute( event : CairngormEvent ) : void {
			var e : ShowReviewEvent = ShowReviewEvent(event);
			 
			( new GetReviewDelegate(this) ).getReview( e.reviewType );
			LifeModelLocator.getInstance().lastReviewType = e.reviewType;
		}
	
	 	public function result( event : Object ) : void {
			var e : ResultEvent = ResultEvent(event);
			var model : LifeModelLocator = LifeModelLocator.getInstance();
			model.currentReviewScreen = makeScreen( e.result, model.lastReviewType );
			model.gotoScreen( LifeModelLocator.SCREEN_REVIEW );
		}
	
		public function fault( event : Object ) : void {
			include "includes/FaultSnippet.as";
		}
		
		
		private function makeScreen( o : Object, tp : String ) : ReviewScreenVO {
	
			var ans : ReviewScreenVO = new ReviewScreenVO();
			ans.lastCompletedOn = o.lastCompleted;
			
			//Set Reviews
			var tmpArr : Array = new Array();
			var i : uint = 0;
			while( i < o.reviews.length ){
				tmpArr[i] = ReviewVO.makeReviewFromObject( o.reviews[i] );
				i++;
			}
			ans.reviews = tmpArr;
			
			//Set Title
			switch( tp ){
				case 'daily': ans.title = 'Daily Review'; 			break;
				case 'weekly': ans.title = 'Weekly Review'; 		break;
				case 'monthly': ans.title = 'Monthly Review'; 		break; 
				case 'quarterly': ans.title = 'Quarterly Review'; 	break;
				case 'yearly': ans.title = 'Yearly Review'; 		break;
			}
			return ans;
		}
	}
}